package com.umeox.moto.watch.fitness.bean;

import android.os.Parcel;
import android.os.Parcelable;

import com.umeox.moto.common.utils.DateUtils;
import com.umeox.moto.watch.fitness.db.model.TodayChallenge;
import com.umeox.moto.watch.fitness.utils.Utils;

import java.util.Calendar;

/**
 * Created by Rambo 2021/11/16
 * Describe:
 */
public class SportChallengeDetail implements Parcelable , Comparable<SportChallengeDetail>, Cloneable {


    /**
     * id : 1
     * sportsType : 1
     * duration : 30
     * reminderTime : 12:00
     * rewardsNumber : 2
     */

    public long id;
    public int sportsType;
    public int duration;
    public String reminderTime;
    public int rewardsValue;

    /**
     * 闹钟具体时间戳，从不重复的闹钟才会把这个值存入数据库，用来判断闹钟是否有效
     */
    public long alarmTimeInMillis;


    public SportChallengeDetail(){

    }

    protected SportChallengeDetail(Parcel in) {
        id = in.readLong();
        sportsType = in.readInt();
        duration = in.readInt();
        reminderTime = in.readString();
        rewardsValue = in.readInt();
    }



    public static SportChallengeDetail convertFromValues(TodayChallenge todayChallenge) {
        if (todayChallenge == null) {
            return null;
        }
        SportChallengeDetail detail = new SportChallengeDetail();
        detail.id = todayChallenge.id;
        detail.sportsType = todayChallenge.sportType;
        detail.duration = todayChallenge.duration;
        detail.reminderTime = todayChallenge.reminderTime;
        detail.rewardsValue = todayChallenge.rewardValue;
        return detail;
    }


    @Override
    public String toString() {
        return "ChallengeBean{" +
                "id=" + id +
                ", sportsType=" + sportsType +
                ", duration=" + duration +
                ", reminderTime='" + reminderTime + '\'' +
                ", rewardsValue=" + rewardsValue +
                ", alarmTimeInMillis=" + alarmTimeInMillis +
                '}';
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(this.id);
        dest.writeInt(this.sportsType);
        dest.writeInt(this.duration);
        dest.writeString(this.reminderTime);
        dest.writeInt(this.rewardsValue);
    }


    public static final Creator<SportChallengeDetail> CREATOR = new Creator<SportChallengeDetail>() {
        @Override
        public SportChallengeDetail createFromParcel(Parcel in) {
            return new SportChallengeDetail(in);
        }

        @Override
        public SportChallengeDetail[] newArray(int size) {
            return new SportChallengeDetail[size];
        }
    };

    @Override
    public int compareTo(SportChallengeDetail another) {
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.HOUR_OF_DAY, getHour());
        cal.set(Calendar.MINUTE, getMinute());
        long temp1 = cal.getTimeInMillis();
        cal.set(Calendar.HOUR_OF_DAY, another.getHour());
        cal.set(Calendar.MINUTE, another.getMinute());
        long temp2 = cal.getTimeInMillis();
        return Long.compare(temp1, temp2);
    }

    public int getHour() {
        return Integer.parseInt(getHourString());
    }

    public String getHourString() {
        if (Utils.is24HourFormatTime(reminderTime)) {
            return reminderTime.split(":")[0];
        } else {
            return DateUtils.formatDateTime(System.currentTimeMillis(), "HH");
        }
    }

    public int getMinute() {
        return Integer.parseInt(getMinuteString());
    }


    public String getMinuteString() {
        if (Utils.is24HourFormatTime(reminderTime)) {
            return reminderTime.split(":")[1];
        } else {
            return DateUtils.formatDateTime(System.currentTimeMillis(), "mm");
        }
    }
}
